// Ensure all relevant items are marked with `pub` keyword

const CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];

// Your Solution here ...
pub trait Anonymize {
    fn anonymize_email(&self) -> String;
}

impl Anonymize for String {
    fn anonymize_email(&self) -> String {
        let mut i = 0_usize;
        let mut r = String::new();
        let mut keep_subfix = false;
        for c in self.chars() {
            if c.eq(&'@') {
                keep_subfix = true;
            }
            if keep_subfix {
                r.push(c);
            } else {
                r.push(CHRISTMAS_EMOJIS[i % 4]);
            }
            i += 1;
        }
        r
    }
}

/* // 1
fn replace_char(prefix: &str) -> String {
    return prefix.chars().map(|c|CHRISTMAS_EMOJIS[(c as usize) % CHRISTMAS_EMOJIS.len()].to_string()).collect::<String>();
}
impl Anonymize for String {
    fn anonymize_email(&self) -> String{
        let parts: Vec<&str> = self.split("@").collect();
        if parts.len() != 2 {
            return format!("{}",replace_char(&self));
        } else {
            return format!("{}@{}", replace_char(parts[0]), parts[1]);
        }
    }
}
*/

/* // 2
static UNTIL: char = '@';
impl Anonymize for String {
    fn anonymize_email(&self) -> String {
        self.chars()
            .zip(CHRISTMAS_EMOJIS.iter().cycle())
            .scan(false, |should_stop, (curr_char, emoji)| {
                if *should_stop || curr_char == UNTIL {
                    *should_stop = true;
                    Some(curr_char)
                } else {
                    Some(*emoji)
                }
            })
            .collect()
    }
}
*/

/* // 3
impl Anonymizer for String {
    fn anonymize_email(&self) -> String {
        let mut result = "".to_string();
        let mut done = false;
        for (i, c) in self.chars().enumerate() {
            if c == '@' {
                done = true;
            }
            if !done {
                result.push_str(&CHRISTMAS_EMOJIS[i % CHRISTMAS_EMOJIS.len()].to_string())
            } else {
                result.push_str(&c.to_string());
            }
        }
        result
    }
}
*/

/* // 4
impl Anonymize for String {
    fn anonymize_email(&self) -> String {
        let mut parts = self.splitn(2, '@');
        let mut email = String::with_capacity(self.capacity());
        if let Some(local) = parts.next() {
            email.push_str(&local
                .chars()
                .enumerate()
                .map(|(idx, _)| CHRISTMAS_EMOJIS[idx % 4])
                .collect::<String>());
        }

        if let Some(domain) = parts.next() {
            email.push_str(&format!("@{}", &domain));
        }
        email
    }
}
*/

/* // 5
fn anonymize(s: &str) -> String {
    let mut anonymized = String::new();
    for i in 0..s.len() {
        anonymized.push(CHRISTMAS_EMOJIS[i % 4]);
    }
    anonymized
}
impl Anonymized for String {
    fn anonymize_email(&self) -> String  {
        if let Some((local, domain)) = self.split_once('@') {
            return format!("{:}@{domain}", anonymize(local)).to_string();
        }
        anonymize(self)
    }
}
*/

/* // 6
impl Anonymize for String {
    fn anonymize_email(&self) -> String {
        let anonymize = |len| CHRISTMAS_EMOJIS.iter().cycle().take(len).collect::<Self>();
        if let Some((prefix, sufix)) = self.split_once('@') {
            format!("{}@{}", anonymize(prefix.len()), sufix)
        } else {
            anonymize(self.len())
        }
    }
}
*/

/* // 7
impl Anonymized for String {
    fn anonymize_email(&self) -> String {
        let mut flag = false;
        self
            .chars()
            .map(|ch| {
                if !flag {
                    if ch == '@' {
                        flag = true;
                        '@'
                    } else {
                        CHRISTMAS_EMOJIS[((ch as u32) % 4) as usize]
                    }
                } else {
                    ch
                }})
            .collect::<String>()

    }
}
*/

/* // 8
impl Anonymize for String {
    fn anonymize_email(&self) -> String {
        let anonymize = |s: &str| CHRISTMAS_EMOJIS
            .iter()
            .copied()
            .cycle()
            .take(s.chars().count())
            .collect::<String>();

        if let Some((local, domain)) = self.split_once('@') {
            let mut email = anonymize(local);
            email.push('@');
            email.push_str(domain);
            email
        } else {
            anonymize(self)
        }
    }
}
*/

/* // 9
impl AnonymizeEmail for String {
    fn anonymize_email(&self) -> String {
        let anonymize = |len| CHRISTMAS_EMOJIS.iter().cycle().take(len).collect::<Self>();
        if let Some((username, domain)) = self.split_once('@') {
            format!("{}@{}", anonymize(username.len()), domain)
        } else {
            anonymize(self.len())
        }
    }
}
*/

/* // 10
impl Anonymized for String {
    fn anonymize_email(&self) -> String {
        match self.split_once("@") {
            Some((f, s)) => {
                format!("{}@{}", (0..f.len()).map(|i| CHRISTMAS_EMOJIS[i % 4]).collect::<String>(), s)
            }
            None => {
                format!("{}", (0..self.len()).map(|i| CHRISTMAS_EMOJIS[i % 4]).collect::<String>())
            }
        }
    }
}
*/

/* // 11
impl Anonymize for String {
    fn anonymize_email(&self) -> String {
        if let Some((username, domain)) = self.split_once('@') {
            let anonymized: String = (0..username.len())
                .map(|i| CHRISTMAS_EMOJIS[i % CHRISTMAS_EMOJIS.len()])
                .collect();
            format!("{}@{}", anonymized, domain)
        } else {
            (0..self.len())
                .map(|i| CHRISTMAS_EMOJIS[i % CHRISTMAS_EMOJIS.len()])
                .collect()
        }
    }
}
*/

pub fn main() {
    let emails = vec![
        "rudolph.therapysessions@northpole.com".to_string(),
        "elfhr.complaint@northpole.urgent".to_string(),
        "santas.rage.management@christmaschaos.noel".to_string(),
        "overtimepay.never@elfexploitation.workshop".to_string(),
        "mrs.claus.divorce.lawyer@northpole.legal".to_string(),
        "reindeer.workers.comp@antler.insurance".to_string(),
        "naughty.list.revenge@santasecrets.com".to_string(),
        "workshop.ptsd.support@elves.anonymous".to_string(),
        "performance.anxiety@santa.breakdown".to_string(),
        "existential.crisis@northpole.void".to_string(),
    ];

    for email in emails {
        let anonymized_email = email.anonymize_email(); // This is the API that Santa wants!
        println!("Original: {} -> Anonymized: {}", email, anonymized_email);
    }
}
